package com.mlnx.ecg.ui.databuffer;

import com.mlnx.ecg.ui.config.ECGConfig;
import com.mlnx.ecg.ui.config.ECGDeviceConstant;
import com.mlnx.ecg.ui.panels.HistoryECGData;

import java.util.ArrayList;
import java.util.List;

public class HistoryEcgDatabuffer {

    private Integer sampling;// 采样率

    private List<List<Object>> bufferList;// 心电数据

    private Integer secondBuffer;

    private byte[] mcuId;

    private Integer writeIndex;// 数据写入下标

    public Integer getSecondBuffer() {
        return secondBuffer;
    }

    public List<List<Object>> getBufferList() {
        return bufferList;
    }

    public HistoryEcgDatabuffer(Integer secondBuffer, List<TimeEcgData> timeEcgData) {

        this.secondBuffer = secondBuffer;
        this.mcuId = mcuId;
        sampling = ECGConfig.SAMPLING;

        // 初始化数据缓存列
        bufferList = new ArrayList<>();

        loadDatabuffer(timeEcgData);
    }

    // 加载数据buffer
    public void loadDatabuffer(List<TimeEcgData> timeEcgData) {
        bufferList.clear();
        for (int index = 0; index < ECGDeviceConstant.ECGCHANNELNUM; index++) {
            List<Object> tempList = new ArrayList<Object>();
            bufferList.add(tempList);
        }

        writeIndex = 0;

        handleUnEncryptedResponse(timeEcgData);
    }


    // 输入心电数据
    public void inputEcgData(Float[] data, Long packetTime) {
        for (int i = 0; i < ECGDeviceConstant.ECGCHANNELNUM; i++) {
            HistoryECGData historyECGData = new HistoryECGData();

            historyECGData.setValue(data[i]);
            historyECGData.setPacketTime(packetTime);
            historyECGData.setNumber(writeIndex);

            bufferList.get(i).add(writeIndex, historyECGData);
        }
        writeIndex++;
    }

    // 处理未加密心电数据
    private void handleUnEncryptedResponse(List<TimeEcgData> timeEcgDataList) {
        for (TimeEcgData timeEcgData : timeEcgDataList) {
            byte[] eightChannelData = new byte[24];

            for (int i = 0; i < timeEcgData.getEcgData().length - 24 + 1; ) {
                for (int j = 0; j < 24; j++) {
                    eightChannelData[j] = timeEcgData.getEcgData()[i++];
                }

                inputEcgData(proData(eightChannelData, false),
                        timeEcgData.getStartTime());
            }
        }
    }

    // 解密后滤波数据
    private Float[] proData(int[] filterData) {

        Float[] mvData = new Float[12];
        mvData[0] = (ad2mv(filterData[0], false));
        mvData[1] = (ad2mv(filterData[1], false));
        mvData[2] = (ad2mv(filterData[1], false) - ad2mv(filterData[0], false));
        mvData[3] = ((float) -0.5 * (ad2mv(filterData[1], false) + ad2mv(
                filterData[0], false)));
        mvData[4] = (ad2mv(filterData[0], false) - (float) 0.5
                * ad2mv(filterData[1], false));
        mvData[5] = (ad2mv(filterData[1], false) - (float) 0.5
                * ad2mv(filterData[0], false));
        mvData[6] = (ad2mv(filterData[2], false));
        mvData[7] = (ad2mv(filterData[3], false));
        mvData[8] = (ad2mv(filterData[4], false));
        mvData[9] = (ad2mv(filterData[5], false));
        mvData[10] = (ad2mv(filterData[6], false));
        mvData[11] = (ad2mv(filterData[7], false));

        return mvData;
    }

    // 未加密数据
    public Float[] proData(byte[] decodedData, boolean original) {

        // saveDataToFile(decodedData);

        List<Integer> data8Channel = new ArrayList<Integer>();

        for (int i = 0; i < decodedData.length; ) {
            int value = 0;

            byte b = decodedData[i++];
            value |= (b & 0x000000ff);
            value <<= 8;

            b = (byte) decodedData[i++];
            // value |= ((b & 0x000000ff) << 8);
            value |= (b & 0x000000ff);
            value <<= 8;

            b = (byte) decodedData[i++];
            // value |= ((b & 0x000000ff) << 16);
            value |= (b & 0x000000ff);

            data8Channel.add(value);
        }

        Float[] mvData = new Float[12];
        mvData[0] = (ad2mv(data8Channel.get(0), original));
        mvData[1] = (ad2mv(data8Channel.get(1), original));
        mvData[2] = (ad2mv(data8Channel.get(1), original) - ad2mv(
                data8Channel.get(0), original));
        mvData[3] = ((float) -0.5 * (ad2mv(data8Channel.get(1), original) + ad2mv(
                data8Channel.get(0), original)));
        mvData[4] = (ad2mv(data8Channel.get(0), original) - (float) 0.5
                * ad2mv(data8Channel.get(1), original));
        mvData[5] = (ad2mv(data8Channel.get(1), original) - (float) 0.5
                * ad2mv(data8Channel.get(0), original));
        mvData[6] = (ad2mv(data8Channel.get(2), original));
        mvData[7] = (ad2mv(data8Channel.get(3), original));
        mvData[8] = (ad2mv(data8Channel.get(4), original));
        mvData[9] = (ad2mv(data8Channel.get(5), original));
        mvData[10] = (ad2mv(data8Channel.get(6), original));
        mvData[11] = (ad2mv(data8Channel.get(7), original));

        return mvData;
    }

    // ad数据转换mv公式
    private float ad2mv(int value, boolean original) {
        if (original)
            return (float) ((value - 0x800000) * 0.476837 / 12f / 1000);

        return (float) ((value - 6000) / 820.0f);
    }

}
